# -*- makefile -*-
# This is the developer's makefile.
# It obeys the environment variables CXX, CFLAGS and CXXFLAGS if you have set them.


GPERF = gperf
CMAKE = cmake
CTEST = ctest
MKDIR = mkdir
RM = rm

CXX ?= g++

CFLAGS := $(CFLAGS)
CXXFLAGS := $(CXXFLAGS) -Ilib

all : lib/generated \
      lib/generated/check-encodings \
      lib/generated/aliases.h \
      lib/generated/indexes.hpp.shippet \
      lib/generated/indexes.h.shippet

lib/generated :
	$(MKDIR) -p lib/generated

lib/generated/check-encodings : tools/check-encodings.cpp
	$(CXX) $(CFLAGS) $(CXXFLAGS) $(LDFLAGS) $< -o $@
	./lib/generated/check-encodings

lib/generated/genaliases : tools/genaliases.cpp
	$(CXX) $(CFLAGS) $(CXXFLAGS) $(LDFLAGS) $< -o $@

lib/generated/aliases.gperf : lib/generated/genaliases
	./lib/generated/genaliases $@

lib/generated/aliases.h : lib/generated/aliases.gperf
	$(GPERF) -L ANSI-C -m 10 $< > $@

lib/generated/genindexes : tools/genindexes.cpp
	$(CXX) $(CFLAGS) $(CXXFLAGS) $(LDFLAGS) $< -o $@

lib/generated/indexes.hpp.shippet : lib/generated/genindexes
	./lib/generated/genindexes C++ > $@

lib/generated/indexes.h.shippet : lib/generated/genindexes
	./lib/generated/genindexes C > $@

test : all
	$(RM) -r -f build tests/data/GB18030-2005.TXT tests/data/GB18030-2022.TXT tests/data/UTF-8.TXT
	$(CMAKE) -B build -S . -DCMAKE_BUILD_TYPE=Debug
	$(CMAKE) --build build --config Debug --target all -j $(shell nproc)
	$(CTEST) --test-dir build --output-on-failure

# Alias to test
check : test

clean :
	$(RM) -r -f build
	$(RM) -r -f cppp-reiconv-*
	$(RM) -r -f .cache
	$(RM) -f lib/generated/aliases.gperf
	$(RM) -f lib/generated/genaliases
	$(RM) -f tests/data/GB18030-2005.TXT
	$(RM) -f tests/data/GB18030-2022.TXT
	$(RM) -f tests/data/UTF-8.TXT
